﻿using DingTalkAgent.DBUtility;
using Li.DingTalk.Helper;
using Li.DingTalk.Model.Contacts;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DingTalkAgent.SqlData
{
    public class T_EmployeeRepository
    {
        #region 批量同步机构信息

            #region 部门
        /// <summary>
        /// 手动批量同步部门
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public int SysDepartments(List<departmentMS> list)
        {
            int result = 0;
            DataTable dt = new DataTable("udtDepartmentMapping");
            dt.Columns.Add("dept_id");
            dt.Columns.Add("name");
            dt.Columns.Add("parentid");
            dt.Columns.Add("order");
            dt.Columns.Add("createDeptGroup");
            dt.Columns.Add("autoAddUser");
            dt.Columns.Add("deptHiding");
            dt.Columns.Add("deptPermits");
            dt.Columns.Add("userPermits");
            dt.Columns.Add("outerDept");
            dt.Columns.Add("outerPermitDepts");
            dt.Columns.Add("outerPermitUsers");
            dt.Columns.Add("orgDeptOwner");
            dt.Columns.Add("deptManagerUseridList");
            foreach (var item in list)
            {
                DataRow row = dt.NewRow();
                row["dept_id"] = item.id;
                row["name"] = item.name;
                row["parentid"] = item.parentid;
                row["order"] = item.order;
                row["createDeptGroup"] = item.createDeptGroup;
                row["autoAddUser"] = item.autoAddUser;
                row["deptHiding"] = item.deptHiding;
                row["deptPermits"] = item.deptPerimits;
                row["userPermits"] = item.userPerimits;
                row["outerDept"] = item.outerDept;
                row["outerPermitDepts"] = item.outerPermitDepts;
                row["outerPermitUsers"] = item.outerPermitUsers;
                row["orgDeptOwner"] = item.orgDeptOwner;
                row["deptManagerUseridList"] = item.deptManagerUseridList;
                dt.Rows.Add(row);
            }
            IDataParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@p_udtDepartmentMapping", dt);
            DbHelperSQL.RunProcedure("AddOrEditDepartmentByBatch", param, out result);
            return result;
        }

        /// <summary>
        /// 服务同步部门
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public int SaveDepartments(List<departmentMS> list,bool isCreate)
        {
            int result = 0;
            DataTable dt = new DataTable("udtDepartmentMapping");
            dt.Columns.Add("dept_id");
            dt.Columns.Add("name");
            dt.Columns.Add("parentid");
            dt.Columns.Add("order");
            dt.Columns.Add("createDeptGroup");
            dt.Columns.Add("autoAddUser");
            dt.Columns.Add("deptHiding");
            dt.Columns.Add("deptPermits");
            dt.Columns.Add("userPermits");
            dt.Columns.Add("outerDept");
            dt.Columns.Add("outerPermitDepts");
            dt.Columns.Add("outerPermitUsers");
            dt.Columns.Add("orgDeptOwner");
            dt.Columns.Add("deptManagerUseridList");
            foreach (var item in list)
            {
                DataRow row = dt.NewRow();
                row["dept_id"] = item.id;
                row["name"] = item.name;
                row["parentid"] = item.parentid;
                row["order"] = item.order;
                row["createDeptGroup"] = item.createDeptGroup;
                row["autoAddUser"] = item.autoAddUser;
                row["deptHiding"] = item.deptHiding;
                row["deptPermits"] = item.deptPerimits;
                row["userPermits"] = item.userPerimits;
                row["outerDept"] = item.outerDept;
                row["outerPermitDepts"] = item.outerPermitDepts;
                row["outerPermitUsers"] = item.outerPermitUsers;
                row["orgDeptOwner"] = item.orgDeptOwner;
                row["deptManagerUseridList"] = item.deptManagerUseridList;
                dt.Rows.Add(row);
            }
            IDataParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@p_udtDepartmentMapping", dt);
            param[1] = new SqlParameter("@p_isCreate", isCreate ? 1 : 0);
            DbHelperSQL.RunProcedure("AddOrEditDepartment", param, out result);
            return result;
        }

        /// <summary>
        /// 批量删除部门
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DelectDepartment(string ids)
        {
            int result = 0;

            IDataParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@p_ids", ids);
            DbHelperSQL.RunProcedure("DeleteDepartments", param, out result);
            return result;
        }

        #endregion

            #region 员工
        /// <summary>
        /// 手动同步员工
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public int SysEmployees(List<departmentmemberuserlists> list)
        {
            int result = 0;
            DataTable dt = new DataTable("udtEmployeeMapping");
            dt.Columns.Add("userid");
            dt.Columns.Add("dingId");
            dt.Columns.Add("mobile");
            dt.Columns.Add("order");
            dt.Columns.Add("isAdmin");
            dt.Columns.Add("isBoss");
            dt.Columns.Add("isHide");
            dt.Columns.Add("isLeader");
            dt.Columns.Add("name");
            dt.Columns.Add("active");
            dt.Columns.Add("department");
            dt.Columns.Add("position");
            dt.Columns.Add("email");
            dt.Columns.Add("orgEmail");
            dt.Columns.Add("avatar");
            dt.Columns.Add("jobnumber");
            dt.Columns.Add("extattr");
            dt.Columns.Add("unionid");
            dt.Columns.Add("openId");
            dt.Columns.Add("hiredDate");
            dt.Columns.Add("remark");
            dt.Columns.Add("tel");
            dt.Columns.Add("workPlace");
            foreach (var item in list)
            {
                DataRow row = dt.NewRow();
                row["userid"] = item.userid;
                row["dingId"] = item.dingId;
                row["mobile"] = item.mobile;
                row["order"] = item.order;
                row["isAdmin"] = item.isAdmin;
                row["isBoss"] = item.isBoss;
                row["isHide"] = item.isHide;
                row["isLeader"] = item.isLeader;
                row["name"] = item.name;
                row["active"] = item.active;
                row["department"] = string.Join(",", item.department); 
                row["position"] = item.position;
                row["email"] = item.email;
                row["orgEmail"] = item.orgEmail;
                row["avatar"] = item.avatar;
                row["jobnumber"] = item.jobnumber;
                row["extattr"] = JsonHelpers.ToJson(item.extattr);
                row["unionid"] = item.unionid;
                row["openId"] = item.openId;
                row["hiredDate"] = item.hiredDate;
                row["remark"] = item.remark;
                row["tel"] = item.tel;
                row["workPlace"] = item.workPlace;
                dt.Rows.Add(row);
            }
            IDataParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@p_udtEmployeeMapping", dt);
            DbHelperSQL.RunProcedure("AddOrEditEmployeeByBatch", param, out result);
            return result;
        }

        /// <summary>
        /// 服务同步员工
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public int SaveEmployees(List<userInfo> list,bool isCreate)
        {
            int result = 0;
            DataTable dt = new DataTable("udtEmployeeMapping");
            dt.Columns.Add("userid");
            dt.Columns.Add("dingId");
            dt.Columns.Add("mobile");
            dt.Columns.Add("order");
            dt.Columns.Add("isAdmin");
            dt.Columns.Add("isBoss");
            dt.Columns.Add("isHide");
            dt.Columns.Add("isLeader");
            dt.Columns.Add("name");
            dt.Columns.Add("active");
            dt.Columns.Add("department");
            dt.Columns.Add("position");
            dt.Columns.Add("email");
            dt.Columns.Add("orgEmail");
            dt.Columns.Add("avatar");
            dt.Columns.Add("jobnumber");
            dt.Columns.Add("extattr");
            dt.Columns.Add("unionid");
            dt.Columns.Add("openId");
            dt.Columns.Add("hiredDate");
            dt.Columns.Add("remark");
            dt.Columns.Add("tel");
            dt.Columns.Add("workPlace");
            foreach (var item in list)
            {
                DataRow row = dt.NewRow();
                row["userid"] = item.userid;
                row["dingId"] = item.dingId;
                row["mobile"] = item.mobile;
                row["order"] = item.orderInDepts;
                row["isAdmin"] = item.isAdmin;
                row["isBoss"] = item.isBoss;
                row["isHide"] = item.isHide;
                row["isLeader"] = item.isLeaderInDepts;
                row["name"] = item.name;
                row["active"] = item.active;
                row["department"] = string.Join(",", item.department);
                row["position"] = item.position;
                row["email"] = item.email;
                row["orgEmail"] = item.orgEmail;
                row["avatar"] = item.avatar;
                row["jobnumber"] = item.jobnumber;
                row["extattr"] = JsonHelpers.ToJson(item.extattr);
                row["unionid"] = item.unionid;
                row["openId"] = null;
                row["hiredDate"] = null;
                row["remark"] = item.remark;
                row["tel"] = item.tel;
                row["workPlace"] = item.workPlace;
                dt.Rows.Add(row);
            }
            IDataParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@p_udtEmployeeMapping", dt);
            param[1] = new SqlParameter("@p_isCreate", isCreate ? 1 : 0);
            DbHelperSQL.RunProcedure("AddOrEditEmployee", param, out result);
            return result;
        }

        /// <summary>
        /// 批量删除员工
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DelectEmployee(string ids)
        {
            int result = 0;

            IDataParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@p_ids", ids);
            DbHelperSQL.RunProcedure("DeleteEmployee", param, out result);
            return result;
        }

        #endregion

        #endregion

        #region 列表展示
        public DataSet GetDepartmentList(string pid)
        {
            IDataParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@p_pid", pid);
            return DbHelperSQL.RunProcedure("GetDepartmentList", param, "department");
        }

        public DataSet GetEmployeeList(int page,int rows,string pid,string query)
        {
            IDataParameter[] param = new SqlParameter[4];
            param[0] = new SqlParameter("@p_page", page);
            param[1] = new SqlParameter("@p_rows", rows);
            param[2] = new SqlParameter("@p_pid", pid);
            param[3] = new SqlParameter("@p_query", query);

            return DbHelperSQL.RunProcedure("GetEmployeeList", param, "employee");
        }
        #endregion

    }
}
